﻿using System.Xml.Linq;
using System.Security.Cryptography.X509Certificates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using AssetManageSystem.Model.AssetManage;
using AssetManageSystem.Model.OrgManage;
using AssetManageSystem.ViewModel.AssetManage.AssetInfoVMs;
using AssetManageSystem.Model.Enum;
using AssetManageSystem.Model;


namespace AssetManageSystem.ViewModel.AssetManage.AssetConsumingVMs
{
    public partial class AssetConsumingVM : BaseCRUDVM<AssetConsuming>
    {
        public List<ComboSelectListItem> AllAsUsers { get; set; }
        public List<ComboSelectListItem> AllUnits { get; set; }

        [Display(Name = "资产选择")]
        public List<string> SelectedAssetIdIDs { get; set; }

        public AssetInfoListVM AssetInfoList { get; set; }

        public AssetConsumingVM()
        {
            SetInclude(x => x.AsUser);
            SetInclude(x => x.Unit);
            SetInclude(x => x.AssetId);
        }

        protected override void InitVM()
        {
            //资产列表
            AssetInfoList = Wtm.CreateVM<AssetInfoListVM>();
            
            AllAsUsers = DC.Set<User>().GetSelectListItems(Wtm, y => y.UserName);
            AllUnits = DC.Set<Department>().GetSelectListItems(Wtm, y => y.DepName);
            SelectedAssetIdIDs = Entity.AssetId?.Select(x => x.AssetId.ToString()).ToList();
            Entity.DocNumber="LY"+Helper.RandomNumber.GetRandom();

        }


        public override void DoAdd()
        {
            Entity.AssetId = new List<AssetConsumMiddleTable>();
            if (SelectedAssetIdIDs != null)
            {
                foreach (var id in SelectedAssetIdIDs)
                {
                     AssetConsumMiddleTable middle = new AssetConsumMiddleTable();
                    middle.SetPropertyValue("AssetId", id);
                    Entity.AssetId.Add(middle);
                }
            }
            //根据领用部门、人员更新资产登记表中相关资产的使用使用部门，同时改变资产状态为在用
            foreach (var sid in SelectedAssetIdIDs)
            {
                var Infos = DC.Set<AssetInfo>().Where(x => x.ID.ToString() == sid).Select(x => x).ToList();
                if (sid != null)
                {
                    Infos.FirstOrDefault().Status = AsStatusEnum.OnUse;
                    Infos.FirstOrDefault().UseDepId = Entity.UnitId;
                    Infos.FirstOrDefault().UsePeopleId = Entity.AsUserId;
                    DC.SaveChanges();
                }
            }
            base.DoAdd();
        }

        public override void DoEdit(bool updateAllFields = false)
        {
            Entity.AssetId = new List<AssetConsumMiddleTable>();
            if(SelectedAssetIdIDs != null )
            {
                 foreach (var item in SelectedAssetIdIDs)
                {
                    AssetConsumMiddleTable middle = new AssetConsumMiddleTable();
                    middle.SetPropertyValue("AssetId", item);
                    Entity.AssetId.Add(middle);
                }
            }

            base.DoEdit(updateAllFields);
        }

        public override void DoDelete()
        {
            base.DoDelete();
        }
    }
}
